package com.archie.zhufengfm.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.Parcelable;

import com.archie.zhufengfm.Constants;
import com.archie.zhufengfm.entity.Track;

import java.io.IOException;
import java.util.List;

public class MusicPlayService extends Service {

    private MediaPlayer mediaPlayer;

    public MusicPlayService() {
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mediaPlayer = new MediaPlayer();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        final String musicPlayUrl = intent.getStringExtra(Constants.SERVICE_TRACK_PLAY_URL);

        new Thread(){

            @Override
            public void run() {

                mediaPlayer.reset();

                try {

                    mediaPlayer.setDataSource(musicPlayUrl);

                    mediaPlayer.prepare();

                    mediaPlayer.start();

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }.start();

        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        if(mediaPlayer.isPlaying()){

            mediaPlayer.stop();

        }

        mediaPlayer.release();
    }
}
